import { createRouter,createWebHashHistory, createWebHistory } from 'vue-router'
import Index from '~/pages/index.vue'
import About from '~/pages/about.vue'
import NotFound from '~/pages/404.vue'
// import Login from '~/pages/login.vue'
import Login from '~/pages/login/index.vue'
import Admin from '~/layouts/admin.vue'
import GoodsList from '~/pages/goods/list.vue'
import CategoryList from '~/pages/category/list.vue'
import UserList from '~/pages/user/list.vue'
import OrderList from '~/pages/order/list.vue'
import CommentList from '~/pages/comment/list.vue'
import ImageList from '~/pages/image/list.vue'
import NoticeList from '~/pages/notice/list.vue'
import SettingList from '~/pages/setting/list.vue'
import CouponList from '~/pages/coupon/list.vue'
import UploadExcel from '~/pages/import-excel/index.vue'
import SysRole from '~/pages/sysRole/index.vue'
import SysUser from '~/pages/sysUser/index.vue'
import Slot from '~/pages/slot/index.vue'
import AssBusiTodo from '~/pages/assBusiTodo/index.vue'

// 默认路由，所有用户都有
const routes = [
{
  path: '/',
  name:'admin',
  component: Admin,
  meta: {
    title: '后台Admin'
  },
  children:[
    {
      path: '/uploadExcel',name: 'uploadExcel',
      component: UploadExcel,
      meta: {
        title: '上传excel'
      }
    }
  ]
},
{
  path: '/about',name: 'about',
  component: About
},
{
  path: '/:pathMatch(.*)*',name: 'NotFound',
  component: NotFound
},
{
  path: '/login',name: 'login',
  component: Login,
  meta: {
    title: '登录页'
  }
},

]

// 动态路由 - 之后匹配用户权限列表动态填充到前端页面
const asyncRoutes = [
  {
    path: '/',
    name: '/',
    component: Index,
    meta: {
      title: '概览'
    }
  },
  {
    path: '/goods/list',
    name: '/goods/list',
    component: GoodsList,
    meta: {
      title: '商品管理'
    }
  },
  {
    path: '/category/list',
    name: '/category/list',
    component: CategoryList,
    meta: {
      title: '分类列表'
    }
  },
  {
    path: '/user/list',
    name: '/user/list',
    component: UserList,
    meta: {
      title: '用户列表'
    }
  },
  {
    path: '/order/list',
    name: '/order/list',
    component: OrderList,
    meta: {
      title: '订单列表'
    }
  },
  {
    path: '/comment/list',
    name: '/comment/list',
    component: CommentList,
    meta: {
      title: '评价'
    }
  },
  {
    path: '/image/list',
    name: '/image/list',
    component: ImageList,
    meta: {
      title: '图库'
    }
  },
  {
    path: '/notice/list',
    name: '/notice/list',
    component: NoticeList,
    meta: {
      title: '公告'
    }
  },
  {
    path: '/setting/base',
    name: '/setting/base',
    component: SettingList,
    meta: {
      title: '配置'
    }
  },
  {
    path: '/coupon/list',
    name: '/coupon/list',
    component: CouponList,
    meta: {
      title: '优惠券'
    }
  },
  {
    path: '/sysRole',
    name: '/sysRole',
    component: SysRole,
    meta: {
      title: '角色列表'
    }
  },
  {
    path: '/sysUser',
    name: '/sysUser',
    component: SysUser,
    meta: {
      title: '用户列表'
    }
  },
  {
    path: '/slot',
    name: '/slot',
    component: Slot,
    meta: {
      title: '实验插槽'
    }
  },
  {
    path: '/assBusiTodo',
    name: '/assBusiTodo',
    component: AssBusiTodo,
    meta: {
      title: '待办事项'
    }
  },

]

export const router = createRouter({
  history: createWebHistory('/shopadmin/'),
  routes
})

// 根据用户权限动态添加路由
export function addRoutes(mesnus){
  let hasNewRoute = false

  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach(item => {
      let t = asyncRoutes.find(item1 => item1.path == item.frontpath)
      if( t && !router.hasRoute(t.path)){
        router.addRoute('admin',t)
        hasNewRoute = true
      }

      if(item.child && item.child.length > 0) findAndAddRoutesByMenus(item.child)
    })
  }

  findAndAddRoutesByMenus(mesnus)
  return hasNewRoute
}
